package com.example.medical.controller.admin;

import com.example.medical.entity.dto.AdminGetLogAffairsDto;
import com.example.medical.entity.dto.LeadershipSaveDeviceDto;
import com.example.medical.entity.dto.LeadershipUpdateDeviceDto;
import com.example.medical.entity.dto.UserGetDeviceListDto;
import com.example.medical.service.DeviceService;
import com.example.medical.service.LogingService;
import com.example.medical.utils.common.enums.Code;
import com.example.medical.utils.common.result.Result;
import com.example.medical.utils.config.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/device")
public class AdminDeviceController {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private LogingService logingService;

    @PostMapping("/get_device_list")
    public Result AdminGetDeviceList(@RequestBody UserGetDeviceListDto dto){
        return deviceService.UserGetDeviceList(dto);
    }

    @GetMapping("/get_device_detail")
    public Result AdminGetDeviceDetail(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(deviceService.getById(id));
    }

    @GetMapping("/remove_device")
    public Result LeadersRemoveDevice(@RequestParam("ids") Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        boolean checkRemove = deviceService.removeById(id);
        if (!checkRemove) {
            return Result.errorResult(Code.SERVER_ERROR);
        }

        return Result.okResult(Code.SUCCESS);
    }

    @GetMapping("/update_get_device_detail")
    public Result AdminUpdateGetDevice(@RequestParam("id") Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(deviceService.getById(id));
    }

    @PostMapping("/update_device")
    public Result AdminUpdateDevice(@RequestBody LeadershipUpdateDeviceDto dto){
        return deviceService.LeadershipUpdateDevice(dto);
    }

    @GetMapping("/remove_device_arr")
    public Result LeadershipRemoveDeviceArr(@RequestParam("ids")String[] ids){
        return deviceService.LeadershipRemoveDeviceArr(ids);
    }

    @PostMapping("/get_log_affairs")
    public Result AdminGetLogAffairs(@RequestBody AdminGetLogAffairsDto dto){
        return logingService.AdminGetLogAffairs(dto);
    }

    @PostMapping("/save_device")
    public Result AdminSaveDevice(@RequestBody LeadershipSaveDeviceDto dto){
        return deviceService.LeadershipSaveDevice(dto);
    }

}
